/*
 * ReplacementGraph.java
 *
 * Created on February 28, 2007, 1:32 PM
 */

package edu.psu.bd.math.vertexreplacer;

import java.util.Iterator;
import java.util.Set;

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.Indexer;
import edu.uci.ics.jung.graph.impl.UndirectedSparseEdge;
import edu.uci.ics.jung.graph.impl.UndirectedSparseVertex;


/**
 *
 * @author Joseph F. Pleso
 */
public class ReplacementGraph {
    
    private int[][] edges;
    private int numEdges;
    private int numBoundary;
    private int numVertices;
    //private double[][] offsets;
    
    /** Creates a new instance of ReplacementGraph */
    public ReplacementGraph(final Graph g) {
        numVertices = g.numVertices();
        numEdges = g.numEdges();
        edges = new int[numEdges][2];
        Vertex[] vertices = new Vertex[numVertices];
        Indexer indexer = Indexer.getAndUpdateIndexer(g);
        numBoundary = 0;
        int top = numVertices - 1;
        for (int i = 0; i < numVertices; i++)
        {
            Vertex v = (Vertex) indexer.getVertex(i);
            
            if (v.getUserDatum("isBoundary") == "true")
            {
                vertices[numBoundary] = v;
                numBoundary++;
            }
            else
            {
                vertices[top] = v;
                top--;
            }
                
                
        }
        
        
        int edgeCount = 0;
                
        for (int i = 0; i < numVertices - 1; i++)
        {
            Set neighbors = vertices[i].getNeighbors();
            Iterator iterator = neighbors.iterator();
            while (iterator.hasNext())
            {
                Vertex next = (Vertex) iterator.next();
                int j;
                for (j = i + 1; j < numVertices; j++)
                {
                    if (next == vertices[j])
                    {
                        edges[edgeCount][0] = i;
                        edges[edgeCount][1] = j;
                        edgeCount++;
                        break;
                    }

                        
                    
                }
            }
        }
    
              
    }
    
    public int getNumBoundary (){
        return numBoundary;
    }
        
    public void replace (Graph g, Vertex v){//, Layout layout ){
        
           // double x = (Double) v.getUserDatum("x");
            //double y = (Double) v.getUserDatum("y");
            
            Vertex[] newVertices = new Vertex[numVertices];

            
            
            int i;
            for (i = 0; i < numVertices; i++)
            {
                newVertices[i] = (Vertex) new UndirectedSparseVertex();
                //newVertices[i].setUserDatum("x", x, UserData.SHARED);
                                
                //newVertices[i].setUserDatum("y", y, UserData.SHARED);
                g.addVertex(newVertices[i]);
            }
                        
            
            
                
            Set neighbors = v.getNeighbors();
            
            
            
            
            
            Iterator iterator = neighbors.iterator();
            int count = 0;
            while (iterator.hasNext())
            {
                Vertex next = (Vertex) iterator.next();
                g.addEdge(new UndirectedSparseEdge(next, newVertices[count]));
                //Double x0 = (Double) next.getUserDatum("x");
                //Double y0 = (Double) next.getUserDatum("y");
                
                //newVertices[count].setUserDatum("x", (x0+x)/2,UserData.SHARED);
                //newVertices[count].setUserDatum("y", (y0+y)/2,UserData.SHARED);
                count++;
            }
            
            for (i = 0; i < numEdges; i++)
            {
                g.addEdge(new UndirectedSparseEdge(newVertices[edges[i][0]],
                        newVertices[edges[i][1]]));
                //Double x0 = (Double) newVertices[edges[i][0]].getUserDatum("x");
                //Double x1 = (Double) newVertices[edges[i][1]].getUserDatum("x");
                //Double y0 = (Double) newVertices[edges[i][0]].getUserDatum("y");
                //Double y1 = (Double) newVertices[edges[i][1]].getUserDatum("y");
                //n/ewVertices[edges[i][1]].setUserDatum("x",(x0+x1)/2, UserData.SHARED);
                //newVertices[edges[i][1]].setUserDatum("y",(y0+y1)/2, UserData.SHARED);
            }
            
            g.removeVertex(v);
        }
        
    
}
